<?php

/**
 * pricecategory
 *
 */
class Globalconfig_Model_Pricecategory extends Zend_Db_Table_Abstract
{
	public $_price = "priceconfig";
    public function __construct($config = array())
    {
        $this->_db = Zend_Db_Table::getDefaultAdapter();
        parent::__construct($config);
    }
	
	public function count($parameter) {
		$db = Zend_Db_Table::getDefaultAdapter();
		$where = "(1=1)";
		
		$sql = "select count(*) as COUNT from " . $this->_price
				. " U WHERE $where";
		$r = $db->query($sql)->fetch();
		return $r['COUNT'];
	}
	public function SelectAll($parameter, $limit, $offset) {
		$db = Zend_Db_Table::getDefaultAdapter();
		$where = "(1=1)";
		$sql = "SELECT 
					*
				FROM " . $this->_price. " U 
				WHERE $where limit $offset,$limit
	                ";
		$r = $db->query($sql)->fetchAll();
		return $r;
	}
	public function SelectAllNoPaging() {
		$db = Zend_Db_Table::getDefaultAdapter();
		
		$sql = "SELECT 
					*
				FROM " . $this->_price;
		$r = $db->query($sql)->fetchAll();
		return $r;
	}
		
	public function AddNewPriceCategory($params) {
		$id = $params['pricecategoryid'];
		$db = Zend_Db_Table::getDefaultAdapter();
		try {
			if($id ==0){
							$data = array(
								'PRICE' => $params['price'],
								'DAYNUMBER' => $params['daynumber'],
								'CURRENCY' => $params['currency']
						);
						$db->insert($this->_price, $data);
			}
			else{
					$data = array(
				            'PRICEID' => $params['priceid'],
				            'PRICE' => $params['price'],
							'DAYNUMBER' => $params['daynumber'],
							'CURRENCY' => $params['currency']
		);
				$db->update($this->_price, $data,"PRICEID = ".$id);
			}
		}catch (Zend_Exception $e) {
			echo ($e->getMessage());
			exit;
		}
	}
	
	public function DeletePriceCategory($id){
		$db = Zend_Db_Table::getDefaultAdapter();
		$where = $db->quoteInto('PRICEID = ?', $id);
		$db->delete($this->_price, $where);
	}
	
	public function FindById($id) {
		$result = $this->getDefaultAdapter()
		->query("SELECT*FROM $this->_price WHERE PRICEID = ?", $id);
		return $result->fetch();
	}
	
	public function CheckExitsID($id) {
		$db = Zend_Db_Table::getDefaultAdapter();
		$sql = "select * from $this->_price where PRICEID='" . $id . "'";
		$stmt = $db->query($sql);
		$r = $stmt->fetch();
		if ($r)
		return 1;
		else
		return 0;
	}

	public function CheckExistsName($price) {
		if($price == '') {
			return 0;
		}
		else 
		{
			$db = Zend_Db_Table::getDefaultAdapter();
			$sql = "select * from $this->_price where PRICE='" . $price . "'";
			$stmt = $db->query($sql);
			$r = $stmt->fetch();
			if ($r)
			return 1;
			else
			return 0;
		}
	}
}

